<script setup lang="ts">
import { ref } from 'vue'
import { mobilerules, passwordrules, coderules } from '@/utils/rules'
import { showSuccessToast, showToast, type FormInstance } from 'vant'
import { useUserStore } from '@/stores'
import { useRoute, useRouter } from 'vue-router'
import { loginByPassword, sendMobileCode, loginByMobile } from '@/services/user'
const userStore = useUserStore()
const router = useRouter()
const route = useRoute()
const toRegister = async () => {
  // console.log('跳转登录页')
}
const ispass = ref(true)
const code = ref('')
const time = ref(0)
const mobile = ref('')
const password = ref('')
const agree = ref(false)
const show = ref(false)
const form = ref<FormInstance | null>(null)
let timeId: number
const login = async () => {
  console.log('点击了登录')
  if (!agree.value) return showToast('请勾选我已同意')
  // 登录功能
  const res = ispass.value
    ? // 发送接口请求
      await loginByPassword(mobile.value, password.value)
    : await loginByMobile(mobile.value, code.value)
  showSuccessToast('登陆成功')
  // 存储用户信息
  userStore.setUser(res.data)
  // 跳转页面
  router.replace((route.query.returnUrl as string) || '/user')
}
const send = async () => {
  // 倒计时大于0,不能发送验证码'
  if (time.value > 0) return
  // 校验手机号
  await form.value?.validate('mobile')
  // 发送验证码
  await sendMobileCode(mobile.value, 'login')
  // 提示用户
  showSuccessToast('发送成功')
  // 开始倒计时
  time.value = 60
  if (timeId) clearInterval(timeId)
  timeId = setInterval(() => {
    time.value--
    if (time.value === 0) clearInterval(timeId)
  }, 1000)
}
</script>

<template>
  <!-- 使用了 unplugin-vue-components 插件后，定义在 components 中的全局组件会自动注册，使用时不需要引入 -->
  <cp-nav-bar title="登录" right-text="注册" @click-right="toRegister"></cp-nav-bar>
  <!-- <div class="login-page">登录页面</div> -->
  <!-- 头部 -->
  <div class="login-head">
    <h3>{{ ispass ? '密码登录' : '短信验证码登录' }}</h3>
    <a href="javascript:;" @click="ispass = !ispass">
      <span>{{ ispass ? '短信验证码登录' : '密码登录' }}</span>
      <van-icon name="arrow"></van-icon>
    </a>
  </div>
  <!-- form 表单 -->
  <!-- form 表单 -->
  <van-form autocomplete="off" @submit="login" ref="form">
    <!-- 手机号 -->
    <van-field
      :rules="mobilerules"
      v-model="mobile"
      name="mobile"
      placeholder="请输入手机号"
      type="text"
    ></van-field>
    <!-- 密码 -->
    <van-field
      v-if="ispass"
      :rules="passwordrules"
      v-model="password"
      placeholder="请输入密码"
      :type="`${show ? 'text' : 'password'}`"
    >
      <template #button>
        <cp-icon :name="`login-eye-${show ? 'on' : 'off'}`" @click="show = !show"></cp-icon>
      </template>
    </van-field>
    <!-- 短信验证码 -->
    <van-field v-else :rules="coderules" v-model="code" placeholder="请输入短信验证码">
      <template #button>
        <span class="btn-send" :class="{ active: time > 0 }" @click="send">
          {{ time > 0 ? `${time}s后再次发送` : '发送验证码' }}
        </span>
      </template>
    </van-field>
    <div class="cp-cell">
      <van-checkbox v-model="agree">
        <span>我已同意</span>
        <a href="javascript:;">用户协议</a>
        <span>及</span>
        <a href="javascript:;">隐私条款</a>
      </van-checkbox>
    </div>
    <div class="cp-cell">
      <van-button block round type="primary" native-type="submit">登 录</van-button>
    </div>
    <div class="cp-cell">
      <a href="javascript:;">忘记密码？</a>
    </div>
  </van-form>
  <!-- 底部 -->
  <div class="login-other">
    <van-divider>第三方登录</van-divider>
    <div class="icon">
      <img src="@/assets/qq.svg" alt="" />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
}
.btn-send {
  color: var(--cp-primary);
  &.active {
    color: rgba(22, 194, 163, 0.5);
  }
}
</style>
